<?php
require_once("bbc.php"); # caching downloader
require_once("arc/ARC2.php");
require_once("LinkedData.php");

$predicates = array(
    array(
        "predicate" => "http://xmlns.com/foaf/0.1/name",
        "catalog"   => "bbc",
        "type"      => "variant",
    ),
    array(
        "predicate" => "http://open.vocab.org/terms/sortLabel",
        "catalog"   => "bbc",
        "type"      => "variant",
    ),
    array(
        "predicate" => "http://purl.org/ontology/mo/image",
        "catalog"   => "bbc",
        "type"      => "image",
    ),
    array(
        "predicate" => "http://purl.org/ontology/mo/biography",
        "catalog"   => "biography",
        "type"      => "link",
    ),
    array(
        "predicate" => "http://purl.org/ontology/mo/wikipedia",
        "catalog"   => "wikipedia",
        "type"      => "link",
    ),
    array(
        "predicate" => "http://purl.org/ontology/mo/imdb",
        "catalog"   => "imdb",
        "type"      => "link",
    ),
    array(
        "predicate" => "http://www.w3.org/2002/07/owl#sameAs",
        "catalog"   => "sameas",
        "type"      => "link",
    ),
);


    $ld = new LinkedData();
    $mblinks = $ld->get_musicbrainz_urls();


    // process the bbc data to find the musicbrainz links and the corresponding BBC urls
    $xml = simplexml_load_file('bbc-musicbrainz-results');
    $map = array();
    foreach ($xml->results->result as $result){
        $bbc = "";
        $mb = "";
        foreach ($result->binding as $binding){
            $name = $binding['name']."";
            if ($name == "x"){
                # subject (bbc uri)
                $bbc = $binding->uri . "";
            } else {
                # musicbrainz url
                $mb = $binding->uri . "";
            }
        }
        if (! (strpos($bbc, "http://api.talis.com") === 0)){
            $map[$mb][] = $bbc;
        }
    }

    $bbcdata = array();

    // loop through musicbrainz links from the latest XML dump and process the bbc files
    foreach (array_keys($mblinks) as $mb){
        $id = $mb; # musicnet id
        foreach ($mblinks[$mb] as $mburl){
            if (array_key_exists($mburl, $map)){
                $bbcurls = $map[$mburl];
                foreach ($bbcurls as $bbcurl){

                    $urldata = get_url_data($bbcurl);

                    $parser = ARC2::getRDFParser();
                    $parser->parse($bbcurl, $urldata);
                    $index = $parser->getSimpleIndex();

                    foreach ($predicates as $def){
                        $predicate = $def['predicate'];
                        $catalog = $def['catalog'];
                        $type = $def['type'];

                        if (array_key_exists($bbcurl, $index) and array_key_exists($predicate, $index[$bbcurl])){
                            foreach ($index[$bbcurl][$predicate] as $value){

                                if ($catalog === "sameas"){
                                    if (strpos($value, "http://dbpedia.org") === 0){
                                        $catalog = "dbpedia";
                                    }
                                }

                                $bbcdata[$mburl][] = array("catalog" => $catalog, "type" => $type, "value" => $value);
                            }
                        }

                    }
                }
            }
        }
    }
    return $bbcdata;
}


?>
